package com.lindenvalley.de.parsers;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.lindenvalley.de.content.Section;

public abstract class AbstractXmlParser {
	
	protected static final String SET = "set";
	private final Pattern pattern = Pattern.compile("&#[0-9]{3}");
	private XmlPullParserFactory factory;
	protected XmlPullParser xpp;
	
	
	
	public AbstractXmlParser() {
		super();
		init();
	}

	protected String decodeFromHtml(String inputString){
		StringBuilder sb = new StringBuilder(inputString);
		Matcher matcher = pattern.matcher(inputString);
		while (matcher.find()){
			  String str = matcher.group();
			  char value = (char)Integer.parseInt(str.substring(2));
			  int index = sb.indexOf(str);
			  sb.replace(index, index + str.length(), String.valueOf(value));			  
		  }
		return sb.toString();
		
	}
	
	protected void init(){
		try {
			factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			xpp = factory.newPullParser();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public abstract Map<Integer, Section> parse(String data);

}
